home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / srcuc.zip / TRANSACT.C < prev    next >
C/C++ Source or Header  |  1990-06-20  |  3KB  |  119 lines

  1. /* Copyright (C) 1990 Free Software Foundation, Inc.
  2.  
  3.    This program is free software; you can redistribute it and/or modify
  4.    it under the terms of the GNU General Public License as published by
  5.    the Free Software Foundation; either version 1, or (at your option)
  6.    any later version.
  7.  
  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.
  12.  
  13.    You should have received a copy of the GNU General Public License
  14.    along with this program; if not, write to the Free Software
  15.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  16.  
  17. /* $Header: transact.c,v 1.1 90/06/20 19:38:56 GMT cph Exp $ */
  18.  
  19. #include <stdio.h>
  20. #include "dstack.h"
  21.  
  22. static void
  23. DEFUN (error, (procedure_name, message),
  24.        CONST char * procedure_name AND
  25.        CONST char * message)
  26. {
  27.   fprintf (stderr, "%s: %s\n", procedure_name, message);
  28.   fflush (stderr);
  29.   abort ();
  30. }
  31.  
  32. enum transaction_state { active, aborting, committing };
  33.  
  34. struct transaction
  35. {
  36.   PTR checkpoint;
  37.   enum transaction_state state;
  38. };
  39.  
  40. static struct transaction * current_transaction;
  41.  
  42. static void
  43. DEFUN (guarantee_current_transaction, (proc), CONST char * proc)
  44. {
  45.   if (current_transaction == 0)
  46.     error (proc, "no transaction");
  47.   switch (current_transaction -> state)
  48.     {
  49.     case committing: error (proc, "commit in progress");
  50.     case aborting: error (proc, "abort in progress");
  51.     }
  52. }
  53.  
  54. void
  55. DEFUN_VOID (transaction_initialize)
  56. {
  57.   current_transaction = 0;
  58. }
  59.  
  60. void
  61. DEFUN_VOID (transaction_begin)
  62. {
  63.   PTR checkpoint = dstack_position;
  64.   struct transaction * transaction =
  65.     (dstack_alloc (sizeof (struct transaction)));
  66.   (transaction -> checkpoint) = checkpoint;
  67.   (transaction -> state) = active;
  68.   dstack_bind ((¤t_transaction), transaction);
  69. }
  70.  
  71. void
  72. DEFUN_VOID (transaction_abort)
  73. {
  74.   guarantee_current_transaction ("transaction_abort");
  75.   (current_transaction -> state) = aborting;
  76.   dstack_set_position (current_transaction -> checkpoint);
  77. }
  78.  
  79. void
  80. DEFUN_VOID (transaction_commit)
  81. {
  82.   guarantee_current_transaction ("transaction_commit");
  83.   (current_transaction -> state) = committing;
  84.   dstack_set_position (current_transaction -> checkpoint);
  85. }
  86.  
  87. struct action
  88. {
  89.   enum transaction_action_type type;
  90.   void EXFUN ((*procedure), (PTR environment));
  91.   PTR environment;
  92. };
  93.  
  94. static void
  95. DEFUN (execute_action, (action), PTR action)
  96. {
  97.   if ((((struct action *) action) -> type) !=
  98.       (((current_transaction -> state) == committing)
  99.        ? tat_abort : tat_commit))
  100.     (* (((struct action *) action) -> procedure))
  101.       (((struct action *) action) -> environment);
  102. }
  103.  
  104. void
  105. DEFUN (transaction_record_action, (type, procedure, environment),
  106.        enum transaction_action_type type AND
  107.        void EXFUN ((*procedure), (PTR environment)) AND
  108.        PTR environment)
  109. {
  110.   guarantee_current_transaction ("transaction_record_action");
  111.   {
  112.     struct action * action = (dstack_alloc (sizeof (struct action)));
  113.     (action -> type) = type;
  114.     (action -> procedure) = procedure;
  115.     (action -> environment) = environment;
  116.     dstack_protect (execute_action, action);
  117.   }
  118. }
  119.